package com.gym.code.config.admin.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Collections;
import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.PreparedStatementCreator;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.support.GeneratedKeyHolder;
import org.springframework.stereotype.Repository;

import com.gym.code.config.admin.bean.table.TableBaseMappingBean;
import com.gym.code.config.admin.dao.ITableMappingDao;

/**
 * 
 * @author xinchun.wang 
   @email: 532002108@qq.com
 * @createTime 2015-4-11 下午5:09:01
 */
@Repository
public class TableMappingDaoImpl implements ITableMappingDao {
	private static final String insertSql = " INSERT INTO table_mapping_info (db_id,table_name,bean_name,dao_name,service_name) VALUES (? ,?, ?,?,?) ";
	private static final String selectSql = "SELECT id, db_id, table_name, bean_name, dao_name, service_name FROM table_mapping_info  ";
	
	private static final String updateSql = " UPDATE table_mapping_info SET table_name = ?,bean_name = ?,dao_name = ?,service_name = ? 	WHERE id = ?";

	@Autowired
	@Qualifier("jdbcTemplate")
	private JdbcTemplate jdbcTemplate;
	
	@Override
	public int insert(TableBaseMappingBean bean) {
		final Object[] args = new Object[] {bean.getDbId(),bean.getTableName(),bean.getBeanName(),bean.getDaoName(),bean.getServiceName()};
		GeneratedKeyHolder keyholder = new GeneratedKeyHolder();
		
		int result = jdbcTemplate.update(new PreparedStatementCreator(){
			@Override
			public PreparedStatement createPreparedStatement(Connection con)
					throws SQLException {
				PreparedStatement statement = con.prepareStatement(insertSql,Statement.RETURN_GENERATED_KEYS);
				for(int i =0;i<args.length;i++){
					statement.setObject(i+1, args[i]);
				}
				return statement;
			}
		}, keyholder);
		
		Number value = keyholder.getKey();
		if(value != null){
			bean.setId(value.longValue());
		}
		
		return result;
	}

	@Override
	public TableBaseMappingBean query(long id) {
		List<TableBaseMappingBean> result = jdbcTemplate.query(selectSql + " where id = ?", new RowMapper<TableBaseMappingBean>() {
			@Override
			public TableBaseMappingBean mapRow(ResultSet rs, int rowNum)
					throws SQLException {
				TableBaseMappingBean bean = new TableBaseMappingBean();
				bean.setId(rs.getLong("id"));
				bean.setDbId(rs.getLong("db_id"));
				bean.setBeanName(rs.getString("bean_name"));
				bean.setServiceName(rs.getString("service_name"));
				bean.setDaoName(rs.getString("dao_name"));
				bean.setTableName(rs.getString("table_name"));
				return bean;
			
			}
		},id);
		if(CollectionUtils.isEmpty(result)){
			return null;
		}
		return result.get(0);
	}

	@Override
	public int update(TableBaseMappingBean bean) {
		Object[] args = new Object[] {bean.getDbId(),bean.getTableName(),bean.getBeanName(),bean.getDaoName(),bean.getServiceName(),bean.getId()};
		return jdbcTemplate.update(updateSql, args);
	}

	@Override
	public List<TableBaseMappingBean> queryBaseAll(long dbId) {
		List<TableBaseMappingBean> result = jdbcTemplate.query(selectSql + " where db_id = ?", new RowMapper<TableBaseMappingBean>() {
			@Override
			public TableBaseMappingBean mapRow(ResultSet rs, int rowNum)
					throws SQLException {
				TableBaseMappingBean bean = new TableBaseMappingBean();
				bean.setId(rs.getLong("id"));
				bean.setDbId(rs.getLong("db_id"));
				bean.setBeanName(rs.getString("bean_name"));
				bean.setServiceName(rs.getString("service_name"));
				bean.setDaoName(rs.getString("dao_name"));
				bean.setTableName(rs.getString("table_name"));
				return bean;
			
			}
		},dbId);
		if(CollectionUtils.isEmpty(result)){
			return Collections.emptyList();
		}
		return result;
	}
	
}
